import pymysql

# 获取连接对象conn，建立数据库的连接
def get_conn():
    conn = pymysql.connect(host="localhost",user="root",passwd="root",db="student")   # db:表示数据库名称
    return conn

def insert(sql, args):
    conn = get_conn()
    cur = conn.cursor()
    result = cur.execute(sql, args)
    conn.commit()
    cur.close()
    conn.close()
    return result

def update(sql,args):
    conn = get_conn()
    cur = conn.cursor()
    result = cur.execute(sql,args)
    conn.commit()
    cur.close()
    conn.close()
    return result

def query(sql,args):
    conn = get_conn()
    cur = conn.cursor()
    cur.execute(sql,args)
    results = cur.fetchall()
    flag = False
    for row in results:
        flag = True
    conn.commit()
    cur.close()
    conn.close()
    return flag

if __name__ == '__main__':
    #插入
    # sql = 'INSERT INTO user VALUES(%s,%s);'
    # insert(sql, ("12345","12345"))

    #更新
    # sql = 'UPDATE user SET passwd=%s WHERE id = %s;'
    # args = ('123456', '12345')
    # update(sql, args)

    #查询
    sql = 'SELECT  * FROM user;'
    query(sql, None)
